﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using umbraco.BusinessLogic;
using System.Net.Mail;
using umbraco;
using PBDesk.Utilities;
using System.Web;

namespace PBDesk.Umbraco.Helper
{
    public class UmbHelper
    {
        #region constants
        public const int TagFilterPageNodeId = 1118;
        public const int ArticlesRootNodeId = 1108;

        #endregion

        #region Query String Readers
        public static T GetQueryStringValue<T>(string qsKey, T defaultValue) 
        {
            T retValue;
            if (defaultValue != null)
                retValue = defaultValue;
            else
                retValue = default(T);
            if (qsKey.IsNotNullAndNotEmpty())
            {
                string qsValue = umbraco.library.RequestQueryString(qsKey);
                if(qsValue.IsNotNullAndNotEmpty())
                {
                    try
                    {
                        retValue = (T)Convert.ChangeType(qsValue, typeof(T));
                    }
                    catch (Exception ex)
                    {
                       WriteExceptionLog("UmbHelper.GetQueryStringValue<T>() for " + qsKey, ex);
                    }
                }

            }
            
            return retValue; 
            
           
        }

        #endregion

        #region Log Writer Methods

        public static void WriteExceptionLog(string methodName, Exception ex)
        {
            Log.Add(LogTypes.Notify, -1, "Error Occured in " +  methodName);
            Exception innerException = ex;
            while (innerException != null)
            {
                Log.Add(LogTypes.Error, -1, innerException.Message);
                innerException = innerException.InnerException;
            }
        }
        #endregion

        #region SendMail() Method

        public static void SendMail(string FromMail, string ToMail, string Subject, string Body, bool IsHtml)
        {
            try
            {
                MailMessage message = new MailMessage(FromMail.Trim(), ToMail.Trim());
                message.From = new MailAddress(FromMail.Trim(), "PBDesk.com");
                message.Subject = Subject;
                if (IsHtml)
                {
                    message.IsBodyHtml = true;
                }
                else
                {
                    message.IsBodyHtml = false;
                }
                message.Body = Body;

                SmtpClient client = new SmtpClient();
                client.EnableSsl = true;
                client.Send(message);
                
            }
            catch (Exception exception)
            {
                Log.Add(LogTypes.Error, -1, string.Format("PBDesk.Umbraco.Helper.UmbHelper.SendMail: Error sending mail. Exception: {0}", exception));
            }
        }



        #endregion

        #region Tags Helper Methods

        public static bool IsTagContained(string tags, string filter)
        {
            
            if (tags.IsNotNullAndNotEmpty() && filter.IsNotNullAndNotEmpty())
            {
                string[] tagArr = StringUtils.GetStringArrFromCSV(tags.ToLower());
                string[] filterArr = StringUtils.GetStringArrFromCSV(filter.ToLower());
                foreach (string filterStr in filterArr)
                {
                    if (Array.IndexOf(tagArr, filterStr) >= 0)
                    {
                        return true;
                    }
                }
            }
            return false;
        }
        public static string DisplayTags(string tagList)
        {
            string retValue = string.Empty;
            if (tagList.IsNotNullAndNotEmpty())
            {
                string[] tagsArr = tagList.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                if (tagsArr != null && tagsArr.Length > 0)
                {
                    foreach (string tag in tagsArr)
                    {
                        if (retValue.IsNotNullAndNotEmpty())
                        {
                            retValue += ", ";
                        }
                        retValue += DisplayTag(tag);
                    }
                }
            }
            return retValue;
        }

        public static string DisplayTag(string tag)
        {
            string retValue = string.Empty;
            if (tag.IsNotNullAndNotEmpty())
            {
                retValue = string.Format("<a href=\"{0}\">{1}</a>", umbraco.library.NiceUrl(TagFilterPageNodeId) + "?filterby=" + HttpUtility.UrlEncode(tag), tag);
            }
            return retValue;
        }
        public static string DisplayTag(string tag, int tagCount)
        {
            string retValue = string.Empty;
            if (tag.IsNotNullAndNotEmpty())
            {
                retValue = string.Format("<a href=\"{0}\">{1}({2})</a>", umbraco.library.NiceUrl(TagFilterPageNodeId) + "?filterby=" + HttpUtility.UrlEncode(tag), tag, tagCount);
            }
            return retValue;
        }
        #endregion
    }
}
